<?php
/**
 * Vypisanie formulara
 * @author Tomas Srna
 * @package bakalarka
 */

class FormWriter implements Writer
{
	protected $form = NULL;
	
	public function __construct(Form $form)
	{
		$this->form = $form;
	}
	
	public function write()
	{
		echo '<form id="editform" action="'.htmlentities($this->form->getAction(), 
			ENT_COMPAT, 'UTF-8').'" '.
			'method="'.$this->form->getMethod().'" class="autocheck"><dl>';
		
		foreach ($this->form->getItems() as $element)
		{
			$this->writeElement($element);
		}
		
		echo '</dl>';

		echo '</form>';
		
		echo '<div>';
		$this->writeButton('disk.png', L::selected()->save, "$('#editform').submit();");
		$this->writeButton('cancel.png', L::selected()->cancel, "cancel();");
		echo '</div>';
		
		$this->writeFunctions();
	}
	
	/**
	 * Vypis tlacitka
	 */
	public function writeButton($img, $title, $fn = "")
	{
?>
<button onclick="<?php echo $fn; ?>">
	<img src="<?php echo APPBASE.'img/'.$img; ?>" alt="" />
	<?php echo $title; ?>
</button>
<?php
	}
	
	public function writeElement(Element $element)
	{
		$this->writeLabel($element);
		switch($element->type)
		{
			case "text":
				$this->writeTextElement($element);
				break;
			case "password":
				$this->writeTextElement($element, true);
				break;
			case "select":
			case "multiselect":
				$this->writeSelectElement($element);
				break;
			case "textarea":
				$this->writeTextareaElement($element);
				break;
		}
		$this->writeError($element);
		$this->writeHint($element);
	}
	
	public function writeLabel(Element $element)
	{
		echo '<dt class="de_'.$element->name.
				'"><label for="'.$element->name.'" class="'.
				$element->class.'">'.$element->label.'</label></dt>';
	}
	
	public function writeHint(Element $element)
	{
		if(!$element->hint)
			return;
		echo '<dd class="hint">'.$element->hint.'</dd>';
	}

	public function writeError(Element $element)
	{
		if(!$this->form->getError($element->name))
			return;
		echo '<dd class="error">'.$this->form->getError($element->name).'</dd>';
	}
	
	public function attr(Element $element)
	{
		
		$classes = explode(" ", $element->class);
		
		$r = "";
		if(array_search("readonly", $classes) !== false)
			$r .= " readonly=\"readonly\"";
		
		if(array_search("disabled", $classes) !== false)
			$r .= " disabled=\"disabled\"";
			
		return $r;
		
	}
	
	public function writeTextElement(Element $element, $password = false)
	{
		$val = htmlentities($this->form->getValue($element->name),
				ENT_COMPAT, 'UTF-8');
		$attr = $this->attr($element);
		$type = ($password ? "password" : "text");
		
		$maxlen = ($element->maxlen ? " maxlength=\"$element->maxlen\"" : "");
		
		echo <<<EOT
<dd class="de_{$element->name}"><input type="$type" name="$element->name" id="$element->name"$maxlen class="$element->class" value="$val"$attr /></dd>
EOT;
	}
	
	public function writeTextareaElement(Element $element)
	{
		$val = $this->form->getValue($element->name);
		
		$attr = $this->attr($element);
		
		echo <<<EOT
<dd class="de_{$element->name}"><textarea name="$element->name" id="$element->name"$attr class="$element->class">$val</textarea></dd>
EOT;

	}
	
	public function writeSelectElement(Element $element)
	{
		$val = $this->form->getValue($element->name);
				
		$attr = $this->attr($element);
		
		echo <<<EOT
<dd class="de_{$element->name}"><select name="$element->name" class="$element->class" id="$element->name"$attr>
EOT;

		$classes = explode(" ", $element->class);
	
		
		if(array_search('required', $classes) === false)
			echo "<option value=\"\">&nbsp;</option>";
			
		foreach ($element->options as $key => $opt)
		{
			$selected = "";
			if($val == $key)
				$selected = " selected=\"selected\"";
			echo <<<EOT
<option value="$key"$selected>$opt</option>
EOT;
		}
		echo <<<EOT
</select></dd>
EOT;
	}
	
	/**
	 * Vypise funkciu pre cancel
	 */
	public function writeFunctions()
	{
?>
<script type="text/javascript">
<!--
	function cancel() { <?php  
		?> window.location.href='<?php
if(array_key_exists('redir', $_GET))
{
	echo $_GET['redir'];
	if(array_key_exists('hash', $_GET))
		echo '#'.$_GET['hash'];
} 
else
	echo APPBASE;
?>'; }
	var submitted = false;
	$(document).ready(function() {
		$('#editform input').keyup(function(e) {
			if(e.keyCode == 13) 
			{
				$(this.form).submit();
			}
		});
		$('#editform input:first').focus();
		
		// Oprava bugu - pri jednom inpute sa posle form 2x
		$('#editform').submit(function (e) {
			submitted = !submitted;
			return submitted;
		});
	});
// -->
</script>

<?php
	}
}
